<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑训练项目</title>
    <script src="../../res/js/jquery-3.4.1.min.js"></script>
    <script src="../../res/layui/layui.js"></script>
    <script src="../../res/js/Tools.js"></script>
    <link rel="stylesheet" href="../../res/layui/css/layui.css">
    <script src="../../res/js/base.js"></script>
</head>

<body>
<script>
    var form = null;
    layui.use(['form', 'layedit', 'laydate'], function() {
        var  layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;

        form = layui.form;


        //日期
        laydate.render({
            elem: '#startTime'
        });
        laydate.render({
            elem: '#endTime'
        });

        loadType();


        //点保存按钮
        $("#saveButton").on("click",function (obj) {

            var expName = $("#exp").val();
            if(expName==""|| expName==null){
                layer.msg("请输入训练项目");
                return;
            }

            var athleteSelect = $("#athleteId").val();
            if(athleteSelect==0){
                layer.msg("请选择运动员");
                return;
            }

            var coachSelect = $("#coachId").val();
            if(coachSelect==0){
                layer.msg("请选择教练员");
                return;
            }

            //获取教练员名称
            $("#coachName").val($('#coachId option:selected').text());
            //获取运动员名称
            $("#athleteName").val($('#athleteId option:selected').text());

            var fromData = getFormData("fromone");//获取表单数据
            var url = getRootPath() + "AtsEmpCoachAthleteController/insertOrUpdate";

            $.post(url,fromData,function (obj) {
                if(obj.num==1){

                    layer.msg(obj.msg, {
                        icon: 1,
                        time: 500 //0.5秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        //关闭当前层
                        //当你在iframe页面关闭自身时
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        //刷新父页面
                        parent.table2.reload();
                    });

                }else{
                    layer.msg(obj.msg);
                }
            },"json");


            return false;
        });
    });

    //加载运动员下拉框
    function loadType(){
        var url = getRootPath() + "AtsEmpAthleteController/findAll";
        var data = null;
        $.post(url,data,function (obj) {
            $.each(obj,function (i,item) {
                $("#athleteId").append("<option value='"+item.athleteId+"'>"+item.athleteName+"</option>");
            });
            form.render();
        },"json");


        var url = getRootPath() + "AtsEmpCoachController/findAll";
        var data = null;
        $.post(url,data,function (obj) {
            $.each(obj,function (i,item) {
                $("#coachId").append("<option value='"+item.coachId+"'>"+item.coachName+"</option>");
            });


            var id = getQueryString("id");
            if (id !="" && id!=null) {
                findByid(id);
            }

            form.render();
        },"json");



    }

    function findByid(id) {
        var url = getRootPath() + "AtsEmpCoachAthleteController/findById";
        var data = {id:id};
        $.post(url,data,function (obj) {
            $("#caId").val(obj.caId);
            $("#athleteId").val(obj.athleteId);
            $("#coachId").val(obj.coachId);
            $("#coachName").val(obj.coachName);
            $("#athleteName").val(obj.athleteName);
            $("#startTime").val(obj.startTime);
            $("#endTime").val(obj.endTime);
            $("#exp").val(obj.exp);
            form.render();
        },"json");
    }

</script>

<form class="layui-form" action="" id="fromone">

    <input id="caId" name="caId" type="hidden">
    <input id="coachName" name="coachName" type="hidden">
    <input id="athleteName" name="athleteName" type="hidden">

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">训练项目</label>
            <div class="layui-input-block">
                <input type="text" name="exp" id="exp" lay-verify="title" autocomplete="off" placeholder="请输入训练项目" class="layui-input" maxlength="128">
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">运动员</label>
            <div class="layui-input-inline">
                <select name="athleteId" id="athleteId" lay-verify="required" lay-search="">
                    <option value=""></option>
                </select>
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">教练员</label>
            <div class="layui-input-inline">
                <select name="coachId" id="coachId" lay-verify="required" lay-search="">
                    <option value=""></option>
                </select>
            </div>
        </div>


        <div class="layui-inline">
            <label class="layui-form-label">起始时间</label>
            <div class="layui-input-block">
                <input type="text" name="startTime" id="startTime" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">结束时间</label>
            <div class="layui-input-block">
                <input type="text" name="endTime" id="endTime" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
            </div>
        </div>

    </div>



    <div class="layui-form-item">
        <div class="layui-input-block">
            <input type="button" value="保存" class="layui-btn" id="saveButton"></input>
        </div>
    </div>

</form>
</body>
</html>